package com.axjy.web;

import com.axjy.pojo.User;
import com.axjy.service.UserService;
import com.axjy.util.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/user")
public class UserHandler {

    @Resource
    private UserService userService;

    private String msg;

    @GetMapping("/list")
    @ResponseBody
    public List<User> list(){
        return userService.list();
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(User user){
        try {
            userService.add(user);
            msg="员工添加成功！";
        }catch (RuntimeException ex){
            msg="员工添加失败!";
            Log.getLogger().error(msg,ex);
        }
        return msg;
    }
    @PostMapping("/update")
    @ResponseBody
    public String update(User user, HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser!=null){
            user.setName(loginUser.getName());
        }
        try{
            userService.update(user);
            msg="员工修改成功！";
        }catch (RuntimeException ex){
            msg="员工修改失败！";
            Log.getLogger().error(msg,ex);
        }
        return msg;
    }

    @PostMapping("/del")
    public String del(Integer id){
        try {
            userService.del(id);
            msg="员工删除成功！";
        }catch (RuntimeException ex){
            msg="员工删除失败！";
            Log.getLogger().error(msg,ex);
        }
        return msg;
    }



//
//    @PostMapping("/batchDel")
//    @ResponseBody
//    public String batchDel(Integer[] ids){
//        try {
//            userService.batchDel(ids);
//            msg="员工批量删除成功！";
//        }catch (RuntimeException ex){
//            msg="员工批量0删除失败！";
//            log.getLogger().error(msg,ex);
//        }
//        return msg;
//    }

}
